//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.decorator;

import javax.swing.Icon;

import org.apache.commons.lang3.StringUtils;
import org.jfab.core.model.CGField;
import org.jfab.core.model.CGInterface;
import org.jfab.core.model.CGMethod;
import org.jfab.core.model.CGMethodDeclaration;
import org.jfab.core.model.CGObject;
import org.jfab.core.model.CGProperty;
import org.jfab.core.model.CGSpec;
import org.jfab.swingui.IconManager;
import org.jfab.swingui.editpane.CGInterfaceEditPane;
import org.jfab.swingui.editpane.EditPane;

/**
 * Provides a decorator implementation for <code>CGInterface</code>.
 * <p>
 * Possible children are:
 * <ul>
 * <li>field</li>
 * <li>property</li>
 * <li>method</li>
 * </ul>
 * </p>
 */
public class CGInterfaceDecorator extends
        CGCompilationUnitDecorator<CGInterface>
{
    /** Child selection values. */
    private static final String[] CHILD_SELECTION_VALUES = { "field",
            "property", "method", };

    /**
     * Construct this object with the given parameter.
     * 
     * @param component Component.
     * @param parent Parent.
     * @param iconManager Icon manager.
     */
    public CGInterfaceDecorator(final CGInterface component,
            final Decorator<?> parent, final IconManager iconManager)
    {
        super(component, parent, iconManager);
    }

    @Override
    public void addNode(final String childTypeName)
    {
        if (StringUtils.isNotEmpty(childTypeName))
        {
            final CGInterface parent = getComponent();
            final CGObject newChild = getModelFactory().create(childTypeName);

            if (newChild instanceof CGField)
            {
                parent.getFields().add((CGField)newChild);
            }
            else if (newChild instanceof CGProperty)
            {
                parent.getProperties().add((CGProperty)newChild);
            }
            else if (newChild instanceof CGMethodDeclaration)
            {
                parent.getMethods().add((CGMethodDeclaration)newChild);
            }
        }
    }

    @Override
    public Object getChild(final int index)
    {
        Object answer;

        final CGInterface cgInterface = getComponent();

        if (index < cgInterface.getProperties().size())
        {
            answer = cgInterface.getProperties().get(index);
        }
        else if (index < (cgInterface.getProperties().size() + cgInterface
                .getFields().size()))
        {
            answer = cgInterface.getFields().get(
                    index - cgInterface.getProperties().size());
        }
        else
        {
            answer = cgInterface.getMethods().get(
                    index - cgInterface.getProperties().size()
                            - cgInterface.getFields().size());
        }

        return answer;
    }

    @Override
    public int getChildCount()
    {
        final CGInterface cgInterface = getComponent();

        return cgInterface.getProperties().size()
                + cgInterface.getFields().size()
                + cgInterface.getMethods().size();
    }

    @Override
    public String[] getChildSelectionValues()
    {
        return CHILD_SELECTION_VALUES;
    }

    @Override
    public EditPane<CGInterface> getEditPane()
    {
        return new CGInterfaceEditPane(getComponent());
    }

    @Override
    public Icon getIcon()
    {
        return getIconManager().getInterfaceIcon();
    }

    @Override
    public int getIndexOfChild(final Object child)
    {
        int answer = -1;

        final CGInterface cgInterface = getComponent();

        if (child instanceof CGProperty)
        {
            final int index = cgInterface.getProperties().indexOf(child);

            if (index >= 0)
            {
                answer = index;
            }
        }
        else if (child instanceof CGField)
        {
            final int index = cgInterface.getFields().indexOf(child);

            if (index >= 0)
            {
                answer = cgInterface.getProperties().size() + index;
            }
        }
        else if (child instanceof CGMethod)
        {
            final int index = cgInterface.getMethods().indexOf(child);

            if (index >= 0)
            {
                answer = cgInterface.getProperties().size()
                        + cgInterface.getFields().size() + index;
            }
        }

        return answer;
    }

    @Override
    public boolean isLeaf()
    {
        final CGInterface cgInterface = getComponent();

        return cgInterface.getProperties().isEmpty()
                && cgInterface.getFields().isEmpty()
                && cgInterface.getMethods().isEmpty();
    }

    @Override
    public boolean removeNode()
    {
        final CGSpecDecorator parent = (CGSpecDecorator)getParent();
        final CGSpec cgSpec = parent.getComponent();

        return cgSpec.getInterfaces().remove(getComponent());
    }
}
